package com.customization.api.e9sso.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.customization.api.e9sso.biz.CusDetailTemplateDownloadBiz;
import com.customization.api.e9sso.biz.CusRequestDetailImport;
import com.customization.api.e9sso.cmd.CusRequestDetailImportCmd;
import com.customization.api.e9sso.service.SsoTokenService;
import com.customization.api.e9sso.service.impl.CusRequestFormServiceImpl;
import com.customization.api.e9sso.service.impl.SsoTokenServiceImpl;
import com.customization.commons.Console;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.workflow.biz.requestForm.DetailTemplateDownloadBiz;
import com.engine.workflow.biz.requestForm.RequestFormBiz;
import com.engine.workflow.service.impl.RequestFormServiceImpl;
import org.apache.commons.lang.StringUtils;
import weaver.file.FileUpload;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.workflow.request.RequestDetailImport;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

public class SsoTokenAction {

    private SsoTokenService getService(User user) {
        return ServiceUtil.getService(SsoTokenServiceImpl.class, user);
    }


    private CusRequestFormServiceImpl getFormService(HttpServletRequest var1, HttpServletResponse var2) {
        User var3 = RequestFormBiz.getFormUser(var1, var2, false);
        return (CusRequestFormServiceImpl)ServiceUtil.getService(CusRequestFormServiceImpl.class, var3);
    }

    @GET
    @Path("/token")
    @Produces({MediaType.TEXT_PLAIN})
    public String getToken(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);
            apidatas.putAll(getService(user).gettokenInfo(ParamUtil.request2Map(request)));

        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }

    @POST
    @Path("/sendMsg")
    @Produces({MediaType.TEXT_PLAIN})
    public String sendMsg(@Context HttpServletRequest request, @Context HttpServletResponse response){
            Map<String, Object> apidatas = new HashMap<String, Object>();
            try {
                //获取当前用户
                User user = HrmUserVarify.getUser(request, response);
                apidatas.putAll(getService(user).sendMsg(ParamUtil.request2Map(request)));

            } catch (Exception e) {
                e.printStackTrace();
                apidatas.put("status", false);
                apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
            }
            return JSONObject.toJSONString(apidatas);
    }

    @POST
    @Path("/updateStatus4Bz")
    @Produces({MediaType.TEXT_PLAIN})
    public String updateStatus4Bz(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);
          //  User user =new User(1); //2021/4/1 10:26 lth    改为固定用户，取消token校验
            apidatas.putAll(getService(user).updateStatus4Bz(ParamUtil.request2Map(request)));

        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }

    @POST
    @Path("/dispatch")
    @Produces({MediaType.TEXT_PLAIN})
    public String dispatch(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);
            apidatas.putAll(getService(user).dispatch(request));

        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }


    @POST
    @Path("/reject")
    @Produces({MediaType.TEXT_PLAIN})
    public String reject(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);
            apidatas.putAll(getService(user).reject(request));
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }

    @POST
    @Path("/submitRequest")
    @Produces({MediaType.TEXT_PLAIN})
    public String submit(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);
            apidatas.putAll(getService(user).submit(request));

        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }



    @POST
    @Path("/getRequestBaseInfo")
    @Produces({MediaType.TEXT_PLAIN})
    public String getRequestBaseInfo(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);
            apidatas.putAll(getService(user).getRequestBaseInfo(ParamUtil.request2Map(request)));

        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }

    @POST
    @Path("/getWorkflowBase")
    @Produces({MediaType.TEXT_PLAIN})
    public String getWorkflowBase(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);
            apidatas.putAll(getService(user).getWorkflowBase(ParamUtil.request2Map(request)));

        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }

    @POST
    @Path("/getToDoWorkflowRequestList")
    @Produces({MediaType.TEXT_PLAIN})
    public String getToDoWorkflowRequestList(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);
            Map<String, Object> params= ParamUtil.request2Map(request);
            params=this.getHeadersInfo(request,params);
            apidatas.putAll(getService(user).getToDoWorkflowRequestList(params));

        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }


    @POST
    @Path("/getHrmSapCompanyList")
    @Produces({MediaType.TEXT_PLAIN})
    public String gethrmSapCompanyList(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);
            Map<String, Object> params= ParamUtil.request2Map(request);
            params=this.getHeadersInfo(request,params);
            apidatas.putAll(getService(user).gethrmSapCompanyList(params));

        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }

    @POST
    @Path("/forwardRequest")
    @Produces({MediaType.TEXT_PLAIN})
    public String forwardRequest(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);
            Map<String, Object> params= ParamUtil.request2Map(request);
            params=this.getHeadersInfo(request,params);
            apidatas.putAll(getService(user).forwardRequest(params));

        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("status", false);
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }




    private Map<String, Object> getHeadersInfo(HttpServletRequest request,Map<String, Object> map) {

        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = (String) headerNames.nextElement();
            String value = request.getHeader(key);
            map.put(key, value);
        }
        return map;
    }

    @GET
    @Path("/dowloadTemplate")
    @Produces({"application/octet-stream"})
    public Response exportExcel(@Context HttpServletRequest var1, @Context HttpServletResponse var2) {
        try {
            CusDetailTemplateDownloadBiz cusDetailTemplateDownloadBiz = new CusDetailTemplateDownloadBiz();
            InputStream var4 = cusDetailTemplateDownloadBiz.templateDownload(var1, var2);
            HrmUserVarify.getUser(var1, var2);

            String var6 = cusDetailTemplateDownloadBiz.getFilename();

            var6 = StringUtils.replace(var6, "/", "");
            var6 = StringUtils.replace(var6, "%2F", "");
            var6 = StringUtils.replace(var6, "+", "%20");
            var6 = Util.formatMultiLang(var6, "7");
            return Response.ok(var4).header("Content-disposition", "attachment;filename=" + var6).header("Cache-Control", "no-cache").build();
        } catch (Exception var7) {
            var7.printStackTrace();
            return null;
        }
    }

    @POST
    @Path("/cusDoImportDetail")
    @Produces({"text/plain"})
    public String cusDoImportDetail(@Context HttpServletRequest request, @Context HttpServletResponse response) {

        User user = HrmUserVarify.getUser(request, response);
        Console.log("1853params:"+ParamUtil.request2Map(request).toString());
        Map var3 = this.getFormService(request, response).requestDetailImport(request);
        return JSON.toJSONString(var3);


    }


}
